<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Order_model extends CI_Model
{
	
	public function __construct()
	{
		parent::__construct();
		$data=array();

	} 	
	public function  get_supplier(){
		$this->db->select('supplier.*');
	$this->db->from('supplier');
//	$this->db->join('users_groups', 'users.id= users_groups.user_id AND users_groups.group_id= 4  '  );
	//$this->db->join('company', 'product.id_company= company.id');
	$q = $this->db->get();
		//$q = $this->db->get_where('customer',array(''=>'');
		if($q->num_rows() > 0) {
			foreach (($q->result()) as $row) {
				$data[] = $row;
			}
				
			return $data;
		}
	}	
	public function get_product_id($code){
	$query = $this->db->get_where('product', array('code' => $code))->row_array();
	
	   return $query['id'];
	
}
    public function insert($data){
			$this->db->insert('purchase_order',$data);
		   return $this->db->insert_id();
    }
	
	public function insert_item($new_data)
	 {
		   $this->db->insert('order_product',$new_data);
	 }
		
		
public function edit_order($id){
	//echo $id;exit;
	$query = $this->db->get_where('purchase_order', array('id' => $id));
	return $query;
	
}

public function edit_products($id){
	
	$query = $this->db->get_where('order_product', array('id_order' => $id));
	return $query;

}		
		
		
		

	public function update($data,$id){ 
	$this->db->update('purchase_order',$data,array('id'=>$id));
		   
	}
	 public function update_item($new_data,$id)
	 { 
	 			   $this->db->update('order_product',$new_data,array('id_order'=>$id));
	 
	 }
	 public function get_product($code)
	 {
	 $query = $this->db->get_where('product', array('code' => $code))->row_array();
	 if(!empty($query['name']))
	 return $query['name']; 
	 
	 }
	 
	 	
public function get_uniqueorder($id){
	$query = $this->db->get_where(' order_product', array('id_order' => $id));
	return $query;
	
}	
	 
	 
	 public function get_order_id()
	 {
	 $this->db->select_max('id');
     $query=$this->db->get('purchase_order')->row_array();
	 if(!empty($query))
	 return $query;
	 else 
	 return 0;
	 }
	  public function get_invoice_code()
	  {
	  $this->db->select('invoice_code');
	  $query=$this->db->get('company_profile')->row_array();
	  if(!empty($query))
	 return $query;
	  
	  }
	 public function get(){
	   $this->datatables
		->select(" purchase_order.id as id, purchase_order.order_no,supplier.name , purchase_order.order_date,purchase_order.expected_date,purchase_order.status")
		->from('purchase_order','supplier' )
		->join('supplier', 'purchase_order.id_supplier= supplier.id' )
			->add_column("Actions", 
			"<center>			<a href='view_unique_order/$1' title='View Details' >
								<i class='icon-print'></i>
								</a>&nbsp;
								   <a href='edit_order/$1' title='Edit Order'>
								<i class='icon-edit'></i>
								</a>&nbsp;
								<a href='delete_order/$1' title='Delete Order' >
								<i class='icon-trash'></i>
								</a>
							    </center>", "id")
			->unset_column('id');
	   return  $this->datatables->generate();
	
}
function delete($id)
{
$this->db->delete('purchase_order',array('id'=>$id));
$this->db->delete('order_product',array('id_order'=>$id));
}
function get_order($id)
{ 
$query = $this->db->get_where('purchase_order', array('id' => $id));
	return $query;

}

public function get_orderno($id){
	$purchase= $this->db->get_where('purchase_order', array('id' => $id))->row_array();
	return $purchase;
	
}


function get_order_product($id)
{
$query = $this->db->get_where('order_product', array('id_order' => $id));
	return $query;

}
function get_product_code($id)
{ 
$query = $this->db->get_where('product', array('id' => $id));
	return $query;

}
public function get_company_profile(){
	
	$query = $this->db->get('company_profile');
	return $query;
}
public function get_customers($id)
{ 
 $query = $this->db->get_where('customer',array('id' =>$id));
	return $query;
}
public function get_all_product($id)
{
 $all_product=$this->db->query ("SELECT  product.id,product.name,product.code,product.selling_price,enquiry_product.quantity
          FROM enquiry_product JOIN product ON product.id =enquiry_product.product_id
        where enquiry_product.enquiry_id='$id'");
		return  $all_product;
}
public function get_tax(){
	$q = $this->db->get('tax');
		if($q->num_rows() > 0) {
			foreach (($q->result()) as $row) {
				$data[] = $row;
			}
				
			return $data;
		}
		
	}	
	public function status($id)
{
$data=array('status' =>'approved');
 $this->db->update('enquiry',$data,array('id'=>$id));

}
public function get_profile(){
		
	$query = $this->db->get('company_profile');

	return $query;
}
public function get_invoicecode($id_branch){
	
	$query = $this->db->get_where('branch', array('id' => $id_branch));	
	return $query;
}
public function get_invoiceno(){
	
	$this->db->from('sales');
	$this->db->order_by("id", "asc");
	$this->db->limit(1);
	$query = $this->db->get(); 
	return $query;

}
public function  get_invoiceid(){
	$this->db->select_max('id');
$query = $this->db->get('sales');
	return $query; 
		
}	
function get_cname($id){
		$query = $this->db->get_where('customer', array('id' => $id))->row_array();
		if(count($query)>0){
				return $query['name'];
		}else{
			return '--';
		}
	
	}


public function insert_sale($data,$id){
		if($data['transaction_type']==0){
			$data['transaction_type']="Cash";
			$this->db->insert('sales',$data);
			$latest_id = $this->db->insert_id();
			$sale_id=array('invoice_no' =>$data['invoice_no'],
			                'status' =>'Billed'  );
			$this->db->update('enquiry',$sale_id,array('id'=>$id));
			/*if($data['customer_name']!=''){
				$data['customer_name']= ' ';	
			}
			*/$data1 = array(
			'id_user' => $data['id_customer'],
			'user_name' => $data['customer_name'],
			'voucher_type' => 'sales',
			'payment_type' => 0,
			'payment_mode' => 'Cash',
			'voucher_no' => $data['invoice_no'],
			'date' => $data['date'],
			'debit_amount' => $data['total_price'],
			'credit_amount' => $data['total_price'],
			'type' => 1,
			'ip' => $_SERVER['REMOTE_ADDR']
			);
			//print_r($data1);exit;
		$this->db->insert('accounts',$data1);
		}else{
			$data['transaction_type']="Credit";
			$this->db->insert('sales',$data);
			$latest_id = $this->db->insert_id();
			$data1 = array(
			'id_user' => $data['id_customer'],
			'user_name' => $data['customer_name'],
			'voucher_type' => 'sales',
			'payment_type' => 0,
			'payment_mode' => '',
			'voucher_no' => $data['invoice_no'],
			'date' => $data['date'],
			'debit_amount' => $data['total_price'],
			'type' => 1,
			'ip' => $_SERVER['REMOTE_ADDR']
			);
			//print_r($data1);exit;
		$this->db->insert('accounts',$data1);
		}
			return $latest_id;
	}



public function insert_sale_item($data,$id_branch){ 
		$product = $this->db->get_where('product', array('id' => $data['id_product']))->row_array();	
		$branch_product = $this->db->get_where('branch_product', array('id_product' => $data['id_product'], 'id_branch' => $id_branch))->row_array();		
			if(($branch_product['opening_stock']>= $data['quantity']) && ($product['opening_stock']>=$data['quantity']))
			{
			$new_qty1 = $branch_product['opening_stock'] - $data['quantity'];
			$data2 = array(
				'opening_stock' => $new_qty1 
			);
		
			$this->db->update('branch_product',$data2,array('id'=>$branch_product['id']));
			$new_qty = $product['opening_stock'] - $data['quantity'];
			$data3 = array(
			'opening_stock' => $new_qty 
			);	
			
		$this->db->update('product',$data3,array('id'=>$data['id_product']));
		$this->db->insert('sales_product',$data);
		}
	}	

public function getID($code){	
	$query = $this->db->get_where('product', array('code' => $code))->row_array();
	return $query['id'];
}

public function search_quantity($code,$qty, $id_branch){ 
	$query1 = $this->db->get_where('product', array('code' => $code));
	$product = $query1->row_array();
	$query = $this->db->get_where('branch_product', array('id_product' => $product['id'],'id_branch' => $id_branch));
	$branch_product = $query->row_array();
	if(($branch_product['opening_stock']>= $qty) && ($product['opening_stock']>=$qty)){
		echo 1;	
	}else if($branch_product['opening_stock']< $qty && $product['opening_stock']>=$qty){
		
		echo 2;
	}else{
		
		echo 3;
	}
	
	
}
public function get_status($id,$qty){
    	$id_branch=$this->session->userdata('id_branch');
	   $query = $this->db->get_where('branch_product', array('id_branch' => $id_branch,'id_product'=>$id))->row_array();
	
	   if($query['opening_stock']>= $qty){return 1;} else {return 0;}
	
}

function supplier_details($id){
			$query = $this->db->get_where('supplier', array('id' => $id))->row_array();
			return $query;	
}	

function profile_details(){
		$query = $this->db->get('company_profile')->row_array();
		return $query;	
	
	
}
	
}